package Acceso;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Puma
 */

public class Conexion {

    private String driver = "com.mysql.jdbc.Driver";
    private String connectString = "jdbc:mysql://localhost:3306/datinvest";
    private String user = "root";
    private String password = "root";
    private Connection conn = null;
    private ResultSet rs = null;
    private Statement stmt = null;

    public Conexion() {
        try {
            Class.forName(driver);
            this.conn = DriverManager.getConnection(connectString, user,password);
           // System.out.println("Conexion a la base de datos realizada con exito!");            
        }
        catch (ClassNotFoundException ex) {
            System.out.println("Clase no encontrada en el driver: " +ex.getMessage());
        }
        catch(SQLException ex) {
            System.out.println("Se ha producido un error en la conexion a la base de datos: "+ex.getMessage());
        }
    }

    public ResultSet realizarConsulta(String query){
        try {
            this.setStmt(this.getConn().createStatement());            
            return this.getStmt().executeQuery(query);
        } catch (SQLException ex) {
            System.out.println("No se pudo realizar la consulta debido a: "+ex.getMessage());
            return null;
        }
    }

    public Boolean realizarInsertUpdateDelete(String query){
     try {
            this.setStmt(this.getConn().createStatement());
            if (this.getStmt().executeUpdate(query) == 1)
                return true;
            else
                return false;
            
        } catch (SQLException ex) {
            System.out.println("No se pudo realizar la consulta debido a: "+ex.getMessage());
            return false;
        }
    }
    
    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

    public String getConnectString() {
        return connectString;
    }

    public void setConnectString(String connectString) {
        this.connectString = connectString;
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public ResultSet getRs() {
        return rs;
    }

    public void setRs(ResultSet rs) {
        this.rs = rs;
    }

    public Statement getStmt() {
        return stmt;
    }

    public void setStmt(Statement stmt) {
        this.stmt = stmt;
    }

}